package com.hnucm.c202001020216.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.databinding.BindingAdapter;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.hnucm.c202001020216.DataApplication;
import com.hnucm.c202001020216.ui.FindCarActivity;
import com.hnucm.c202001020216.ui.LookCarActivity;
import com.hnucm.c202001020216.R;
import com.hnucm.c202001020216.utils.RetrofitUtils;
import com.hnucm.c202001020216.api.CarApi;
import com.hnucm.c202001020216.databinding.KeywordcaritemBinding;
import com.hnucm.c202001020216.pojo.Car;


import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;



public class ChooseFragment extends Fragment {


    MyAdapter myAdapter;
    RecyclerView recyclerView;

    EditText findEdt;
    ImageView findBtn;

    String keyword="玛莎拉蒂";
    Car allCar=new Car();


    public List<Car.PageDTO.ListDTO> carList=new ArrayList<>();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_choose, container, false);

        //汽车列表
        recyclerView=view.findViewById(R.id.recyclerView3);

        findEdt=view.findViewById(R.id.editTextTextPersonName2);
        findBtn=view.findViewById(R.id.imageView3);

        //1.请求数据
        CarApi api= RetrofitUtils.getRetrofit(DataApplication.HttpURL).create(CarApi.class);
        Call<Car> itemCall=api.getAllCar("奥迪");
        //获取网络数据
        itemCall.enqueue(new Callback<Car>() {
            @Override
            public void onResponse(Call<Car> call, Response<Car> response) {
                allCar=response.body();
                carList.clear();
                carList.addAll(allCar.page.list);

                myAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<Car> call, Throwable t) {

            }
        });

        myAdapter =new MyAdapter();
        recyclerView.setAdapter(myAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false));


        //按钮监听
        findBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                keyword=findEdt.getText().toString();

                Intent intent=new Intent(getActivity(), FindCarActivity.class);
                intent.putExtra("allCar",allCar);
                startActivity(intent);

                }
        });
        return view;
    }


    public class MyAdapter extends RecyclerView.Adapter<MyViewHolder>{

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            KeywordcaritemBinding keywordcaritemBinding=DataBindingUtil.inflate(LayoutInflater.from(getActivity()),
                    R.layout.keywordcaritem,parent,false);
            MyViewHolder myViewHolder=new MyViewHolder(keywordcaritemBinding.getRoot());
            myViewHolder.keywordcaritemBinding=keywordcaritemBinding;
            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
            holder.keywordcaritemBinding.setCar(carList.get(position));
            holder.keywordcaritemBinding.materialCardView40.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(getActivity(), LookCarActivity.class);
                    intent.putExtra("keyCar",allCar.page.list.get(position));
                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return carList.size();
        }
    }


    public class MyViewHolder extends RecyclerView.ViewHolder{

        KeywordcaritemBinding keywordcaritemBinding;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    //自定义ImageView属性
    @BindingAdapter("imageUrl")
    public static void bindImageUrl(ImageView view, String url){
        Glide.with(view)
                .load(url)
                .into(view);
    }

}